home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 10 / FM Towns Free Software Collection 10.iso / ms_dos / tool / fapxtool / src / txl / txlnpcut.c < prev    next >
C/C++ Source or Header  |  1995-02-12  |  5KB  |  259 lines

  1. /***************
  2. *
  3. * g:\exe\txl\src\txlnpcut.c
  4. */
  5. #include "txl.h"
  6.  
  7. void result()
  8. {
  9.     if (make_val > 0) {
  10.         fprintf(fpmes, "\n%4d file(s) picked !\n",make_val);
  11.     }
  12.     else {
  13.         fprintf(fpmes, "\n no files picked.\n");
  14.     }
  15. }
  16.  
  17. void make_new_name(char *tellptr)
  18. {
  19.     char tellchr;
  20.     struct find_t filedata;
  21.  
  22.     if (tellptr == NULL) {
  23.         tellptr = strchr(line1, NUL) - 1;
  24.     }
  25.     tellchr = *tellptr;
  26.     tellchr++;
  27.  
  28.     if (tellchr == '\\') {
  29.         fprintf(fpmes, "Error:increment point is overrun the filename.\n");
  30.         result();
  31.         Exit(1);
  32.     }
  33.     if (isalnum((char)tellchr)) {
  34.         *tellptr = tellchr;
  35.     }
  36.     else if (tellchr == ':') {
  37.         *tellptr = '0';
  38.         make_new_name(tellptr - 1);
  39.     }
  40.     else if ((tellchr=='{') || (tellchr=='[')) {
  41.         *tellptr = tellchr - 26;
  42.         make_new_name(tellptr - 1);
  43.     }
  44.     else {
  45.         fprintf(fpmes, "Error:cannot make new filename...\n");
  46.         Exit(1);
  47.     }
  48.     if (_dos_findfirst(line1, _A_ARCH | _A_NORMAL | _A_RDONLY, &filedata)
  49.         == 0) {
  50.         make_new_name(strrchr(line1, '.') - 1);
  51.     }
  52. }
  53.  
  54. int findfront(FILE **input, FILE **output)
  55. {
  56.     int i = 0, chr = NUL;
  57.     struct find_t filedata;
  58.  
  59.     while (i < sizeof(npcut_front)) {
  60.         chr = getc(*input);
  61.         if (chr == npcut_front[i]) {
  62.             i++;
  63.         }
  64.         else if (chr == 0x0d) {
  65.             chr = getc(*input);
  66.             if (chr != 0x0a) i = 0;
  67.         }
  68.         else {
  69.             i = 0;
  70.         }
  71.         if (chr == EOF) {
  72.             return (-1);
  73.         }
  74.     }
  75.     if (_dos_findfirst(line1, _A_ARCH | _A_NORMAL | _A_RDONLY, &filedata)
  76.         ==0) {
  77.         make_new_name(strchr(line1, '.') - 1);
  78.     }
  79.     if (*line1 != NUL) {
  80.         *output = fopen(line1, "wb");
  81.     }
  82.     else {
  83.         *output = NULL;
  84.     }
  85.     if (*output == NULL) {
  86.         fprintf(fpmes, "File=%s\n", line1);
  87.         errexit("cannot open line1");
  88.     }
  89.     return (0);
  90. }
  91.  
  92. int findtell(int endflg, FILE **input, FILE **output)
  93. {
  94.     int i=0, chr=NUL, j=0, k=0, diff=0, same=0, l=0;
  95.     char tmpbuf[24];
  96.     char *delstr[NP_DEL], *delptr[NP_DEL];
  97.  
  98.     delstr[0] = npcut_tellstr;
  99.     delstr[1] = npcut_niffirst;
  100.     delstr[2] = npcut_nifend;
  101.     delstr[3] = npcut_weather;
  102.     weatherflg = 0;
  103.  
  104.     for (j = 0; j < NP_DEL; j++) {
  105.         delptr[j] = delstr[j];
  106.     }
  107.  
  108.     if (endflg) return (-1);
  109.  
  110.     while (i < (sizeof(npcut_tellstr) - 1)) {
  111.         chr = getc(*input);
  112.         if (chr == EOF) {
  113.             return (-1);
  114.         }
  115.         if (chr == npcut_tellstr[i]) {
  116.             i++;
  117.         }
  118.         else if (chr == 0x0d) {
  119.             putc(0x0d, *output);
  120.             chr = getc(*input);
  121.             if (chr != 0x0a) {
  122.                 i = 0;
  123.             }
  124.         }
  125.         else {
  126.             i = 0;
  127.         }
  128.  
  129.         diff = 0;
  130.         same = 0;
  131.         for (k = 0; k < NP_DEL; k++) {
  132.             if (chr == *delptr[k]) {
  133.                 delptr[k] += 1;
  134.                 if (*delptr[k] == NUL) {
  135.                     same = 1;
  136.                     delptr[k] = delstr[k];
  137.                     if (k == 3) {
  138.                         weatherflg = 1;
  139.                     }
  140.                 }
  141.             }
  142.             else {
  143.                 delptr[k] = delstr[k];
  144.                 diff++;
  145.             }
  146.         }
  147.         if (same == 0) {
  148.             if (diff < NP_DEL) {
  149.                 tmpbuf[l] = chr;
  150.                 l++;
  151.                 tmpbuf[l] = NUL;
  152.             }
  153.             else {
  154.                 if (weatherflg == 1) {
  155.                     fclose(*output);
  156.                     fprintf(fpmes, "%s\n", line1);
  157.                     make_val += 1;
  158.                     make_new_name(strrchr(line1, '.') - 1);
  159.                     if((*output = fopen(line1,"wb")) == NULL) {
  160.                         fprintf(fpmes, "Error:cannot open line1\n");
  161.                         Exit(0);
  162.                     }
  163.                     weatherflg = 0;
  164.                 }
  165.                 if (l > 0) {
  166.                     fwrite(tmpbuf, 1, l, *output);
  167.                 }
  168.                 putc((char)chr, *output);
  169.                 l = 0;
  170.             }
  171.         }
  172.         else {
  173.             l = 0;
  174.         }
  175.     }
  176.  
  177.     fclose(*output);
  178.     fprintf(fpmes, "%s\n", line1);
  179.     make_val += 1;
  180.     make_new_name(strrchr(line1, '.') - 1);
  181.  
  182.     return (0);
  183. }
  184.  
  185. void npcut()
  186. {
  187.     FILE *input, *output = NULL;
  188.     int ret = 0;
  189.     if (strchr(line1,'.') == NULL) {
  190.         strcat(line1, ".NPB");
  191.     }
  192.  
  193.     if (*inputfile != NUL) {
  194.         input = fopen(inputfile, "rb");
  195.     }
  196.     else {
  197.         errexit("Cannot open inputfile.");
  198.     }
  199.     if (input == NULL) {
  200.         errexit("cannot open inputfile");
  201.     }
  202.  
  203.     while (!ret) {
  204.         ret = findfront(&input, &output);
  205.         ret = findtell(ret, &input, &output);
  206.     }
  207.     result();
  208. }
  209.  
  210. void npcutinit()
  211. {
  212.     fprintf(stderr, "TXF inline module. NPCUT Ver1.7\n");
  213.     strcpy(line1, "TXF00000.NPB");
  214.  
  215. }
  216.  
  217. void npcutdriver(char *val[])
  218. {
  219.     int i = 0;
  220.  
  221.     npcutinit();
  222.     for (i = 0; val[i] != NULL; i++) {
  223.         if (val[i][0] == '-') {
  224.             switch (val[i][1]) {
  225.             case 'i':
  226.             case 'I':
  227.                 if (val[i+1][0] != '-') {
  228.                     inputfile = val[i+1];
  229.                 }
  230.                 else {
  231.                     i--;
  232.                 }
  233.                 break;
  234.             case 'o':
  235.             case 'O':
  236.                 if (val[i+1][0] != '-') {
  237.                     strcpy(line1,val[i+1]);
  238.                 }
  239.                 else {
  240.                     i--;
  241.                 }
  242.                 break;
  243.             default:
  244.                 fprintf(stderr, "Error:invailed parameter'%Fs'\n", val[i]);
  245.                 Exit(1);
  246.             }
  247.             i++;
  248.         }
  249.         else if (inputfile == NULL) {
  250.             inputfile = val[i];
  251.         }
  252.         else if (inputfile != NULL) {
  253.             strcpy(line1,val[i]);
  254.         }
  255.     }
  256.     npcut();
  257.     Exit(0);
  258. }
  259.